import time
from multiprocessing import Pool
import datetime

# 代理服务器
proxyHost = "http-pro.abuyun.com"
proxyPort = "9010"

# 代理隧道验证信息
proxyUser = "H70B99V9Z752485P"
proxyPass = "67CA67814D3A9E21"

service_args = [
    "--proxy-type=http",
    "--proxy=%(host)s:%(port)s" % {
        "host": proxyHost,
        "port": proxyPort,
    },
    "--proxy-auth=%(user)s:%(pass)s" % {
        "user": proxyUser,
        "pass": proxyPass,
    },
]

import jd


def single_process_cralwer(task):
    # fn: 函数参数是数据列表的一个元素
    logo = task['logo']
    url = task['url']
    print(datetime.datetime.today(), '开始处理%s，url=%s' % (logo, url))
    jd.search_by_url(logo=logo, url=url)


if __name__ == "__main__":
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from selenium import webdriver

    dcap = dict(DesiredCapabilities.PHANTOMJS)
    dcap["phantomjs.page.settings.userAgent"] = (
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
    # 设置user-agent请求头
    dcap["phantomjs.page.settings.loadImages"] = False  # 禁止加载图片
    driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=service_args)
    driver.implicitly_wait(20)
    driver.set_page_load_timeout(20)
    driver.get('https://www.jd.com')
    driver.find_element_by_id('key').send_keys('手机')
    driver.find_element_by_class_name('button').click()
    time.sleep(5)
    aes = driver.find_elements_by_css_selector(
        'div#J_selector div.sl-v-logos a')

    list = []
    for a in aes:
        print(a.get_attribute('title') + ':' + a.get_attribute('href'))
        dict = {}
        dict['logo'] = a.get_attribute('title')
        dict['url'] = a.get_attribute('href')
        list.append(dict)
    driver.quit()
    pool = Pool(5)
    for task in list :
        pool.apply_async(single_process_cralwer ,(task,))
    #rl = pool.map_async(single_process_cralwer, list)
    pool.close()
    pool.join()
    print('任务完成')
